﻿<?php
namespace app\services\weixin\pay;


require_once __DIR__ . "/lib/WxPayData.php";
require_once __DIR__ . "/lib/WxPayNotify.php";
require_once __DIR__ . "/lib/WxPayApi.php";
require_once __DIR__ . "/login.php";
require_once __DIR__ . "/db.php";

/**
 * 微信支付回调
 * Created by PhpStorm.
 * User: fanxu(746439274@qq.com)
 * Date: 2015/8/5
 * Time: 15:15
 */


class WeiXinPayCallBack extends \WxPayNotify
{
    /**
     * 查询订单
     * @Author fanxu(746439274@qq.com)
     * @param $transaction_id
     * @return bool
     * @throws \WxPayException
     */
    public function QueryOrder($transaction_id)
    {
        $input = new \WxPayOrderQuery();
        $input->SetTransaction_id($transaction_id);
        $result = \WxPayApi::orderQuery($input);
        if(array_key_exists("return_code", $result)
            && array_key_exists("result_code", $result)
            && $result["return_code"] == "SUCCESS"
            && $result["result_code"] == "SUCCESS")
        {
            return true;
        }
        return false;
    }

    /**
     * 重写回调处理函数
     * @Author fanxu(746439274@qq.com)
     * @param array $data
     * @param string $msg
     * @return bool
     */
    public function NotifyProcess($data, &$msg)
    {
        if(!array_key_exists("transaction_id", $data)){
            $msg = "输入参数不正确";
            return false;
        }
        //查询订单，判断订单真实性
        if(!$this->QueryOrder($data["transaction_id"])){
            $msg = "订单查询失败";
            return false;
        }
        //自己平台订单号
        $orderId = $data['attach'];
        \Mysql::getInstance()->query('select * from lw_recharge_log where order_id="' . $orderId . '" and status = 0 ' );
        $order = \Mysql::getInstance()->fetch_assoc();
        //查找订单 更新订单状态
        $openid = $order['openid'];
        $result = getSession( $openid );
        $vipid = $result['VipId'];
        $sid = $result['sessionId'];
        $url = 'http://116.62.191.191:8091/MallInterface/MallRecharge.aspx';
        $url .= '?sid='.$sid.'&vipid='.$vipid.'&SumCash='.($order['money']*100).'&code='.$openid.'&type=1';
		//echo $url ;
        //file_get_contents( $url );
        //file_put_contents( 'recharge.txt' , $url );

		//$content = file_get_contents("http://localhost/a.php");//得到文件执行的结果
		$of = fopen('dir.txt','w');//创建并打开dir.txt
		if($of){
		 fwrite($of,$url);//把执行文件的结果写入txt文件
		}
		fclose($of);

        //设置支付时间
        \Mysql::getInstance()->query('update lw_recharge_log set status = 1 where order_id="' . $orderId . '"' );
        return true;
    }
}
